package com.chenglu.android.common.luanch;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chenglu.android.MainActivity;
import com.chenglu.android.R;
import com.chenglu.android.bean.LoginBean;
import com.chenglu.android.common.base.BaseApplication;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.SPUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.subjects.Subject;


/**
 * @author: lyj
 * @Created: by Copyright 2023/3/3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：启动页
 */
public class LaunchActivity extends BaseFullScreenActivity {



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
//        setTheme(R.style.SplashTheme);

    }


    @Override
    protected void initView() {
        super.initView();
        initSp();
    }

    /**
     * @description 倒计时
     */

    private void initSp() {

        int anInt = SPUtils.getInt(Constant.SPPRIVACY, 0);
        if (anInt == 1001) {
            Global.getMainHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(Global.mContext, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            },500);


        } else {
            //弹窗显示协议
            showPrivacy();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void showPrivacy() {
        PrivacyPopup customPopup = new PrivacyPopup(this);
        new XPopup.Builder(this)
                .dismissOnTouchOutside(false)
                .asCustom(customPopup)
                .show();

    }


    class PrivacyPopup extends BottomPopupView {
        //注意：自定义弹窗本质是一个自定义View，但是只需重写一个参数的构造，其他的不要重写，所有的自定义弹窗都是这样。
        public PrivacyPopup(@NonNull Context context) {
            super(context);
        }

        // 返回自定义弹窗的布局
        @Override
        protected int getImplLayoutId() {
            return R.layout.dialog_privacy;
        }

        // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
        @Override
        protected void onCreate() {
            super.onCreate();

            TextView textView = findViewById(R.id.tv_privacy);

            TextView tv_connect_use = findViewById(R.id.tv_connect_use);
            tv_connect_use.setText("为了提供优质的购物体验，" + getResources().getString(R.string.app_name) + "App需要联网并使用您的位置信息，以获取周边商品库存、优化配送服务和进行个性化商品推荐您有权拒绝或取消授权");

            TextView tv_connect_login = findViewById(R.id.tv_connect_login);

            tv_connect_login.setText( getResources().getString(R.string.app_name) +"App需要使用个人账号，且个人账号需要联网，以便为您提供便捷的注册、登录服务，您有权拒绝或取消授权");

//
            final String name1 = " 在使用App之前，请您阅读并充分理解，";
            final String linkWord1 = "《" +getResources().getString(R.string.app_name) + "用户协议》";
            String name2 = "和";

            final String linkWord2 = "《 " + getResources().getString(R.string.app_name)   +"隐私协议》";

            // 19   29
            String name3 = "，了解您的用户权益及相关数据的处理方法，如您同意以上的协议内容，" +
                    "请点击同意，开始使用我们的产品和服务。感谢您的信任。";

            String word = name1 + linkWord1 + name2 + linkWord2 + name3;

            SpannableStringBuilder ss = new SpannableStringBuilder(word);

            int index1 = word.indexOf(linkWord1);
            int index2 = word.indexOf(linkWord2);

            ss.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(Constant.HTTPYONGHU));
                    startActivity(intent);

                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.parseColor("#F87E09"));     //设置文件颜色
                    ds.setUnderlineText(false);      //设置下划线
                }
            }, index1, index1 + linkWord1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            ss.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(Constant.HTTPYINSI));
                    startActivity(intent);
//                    ToastUtils.show("dddddddd");
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.parseColor("#F87E09"));       //设置文件颜色
                    ds.setUnderlineText(false);      //设置下划线
                }
            }, index2, index2 + linkWord2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            textView.setText(ss);
            textView.setMovementMethod(LinkMovementMethod.getInstance());


            Button btnAgree = findViewById(R.id.btn_agree);

            btnAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Global.mContext, MainActivity.class);

                    startActivity(intent);
                    SPUtils.setInt(Constant.SPPRIVACY, 1001);
                    finish();

                }
            });
            Button btnNoAgree = findViewById(R.id.btn_no_agree);

            btnNoAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });


        }

        // 设置最大宽度，看需要而定，
        @Override
        protected int getMaxWidth() {
            return super.getMaxWidth();
        }

        // 设置最大高度，看需要而定
        @Override
        protected int getMaxHeight() {
            return super.getMaxHeight();
        }

        // 设置自定义动画器，看需要而定
        @Override
        protected PopupAnimator getPopupAnimator() {
            return super.getPopupAnimator();
        }

        /**
         * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
         *
         * @return
         */
        protected int getPopupWidth() {
            return 0;
        }

        /**
         * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
         *
         * @return
         */
        protected int getPopupHeight() {
            return 0;
        }


    }

}
